import 'package:find_friend/common/share.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/xspace/detail/xcomment_voice.dart';
import 'package:find_friend/pages/xspace/model/artclemodel.dart';
import 'package:find_friend/pages/xspace/model/school_model.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart' as native;

class AlertTool {
  static showLoadingDialog(BuildContext context, [String loadingText]) {
    showDialog(
      context: context,
      barrierDismissible: false, //点击遮罩不关闭对话框
      builder: (context) {
        return Container(
          margin: const EdgeInsets.symmetric(horizontal: 50),
          child: AlertDialog(
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  width: 30,
                  height: 30,
                  child: Image.asset("assets/circles-menu.gif", color: APP_main_color,)),
                Padding(
                  padding: const EdgeInsets.only(top: 26.0),
                  child: Text(loadingText != null ? loadingText : "正在加载，请稍后...", style: instanceTextStyle(14, APP_font_black),),
                )
              ],
            ),
          ),
        );
      },
    );
  }

  ///
  ///文字选择框
  static showBottomDialog(BuildContext context, List<String> titles,{Function(int index) onTapIndex}) {
    final size = MediaQuery.of(context).size;

    List<Widget> list = [];
    for (var i = 0; i < titles.length; i++) {
      Widget wid = GestureDetector(
                    onTap: (){
                      if(onTapIndex != null) onTapIndex(i);
                    },
                    child: Container(
                      color: Colors.white,
                      margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                      alignment: Alignment.center,
                      child: Text(titles[i], style: instanceTextStyle(14, APP_font_black),)
                    ),
                  );
      list.add(wid);
      if(i != titles.length - 1)list.add(Divider(height: 0.5,endIndent: 42, indent: 42,));
    }

    showModalBottomSheet(
      backgroundColor: Colors.transparent,
      context: context,
      builder: (context) {
        return SafeArea(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Container(
                  width: size.width - 50,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.white,
                  ),
                  padding: const EdgeInsets.symmetric(horizontal: 0),
                  child: Column(
                    children: list
                  ),
                ),
                SizedBox(height: 10,),
                GestureDetector(
                  onTap: () => Navigator.of(context).pop(),
                  child: Container(
                    width: size.width - 50,
                    padding: const EdgeInsets.symmetric(vertical: 8.5),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      color: Colors.white,
                    ),
                    child: Text("取消", style: instanceTextStyle(14, APP_font_black),)
                  ),
                ),
                SizedBox(height: 10,),
              ],
            ),
          ),
        );
      }
    );
  } 

  /// 
  /// 分享弹窗
  static showBottomShareDialog(BuildContext context, {ArticleModel articleModel, ReplyModel replyModel, Function() onRefreshData}) {
    showModalBottomSheet(
      backgroundColor: Colors.transparent,
      context: context,
      builder: (context) {
        return ShareWidget(replyModel: replyModel, articleModel: articleModel,onRefreshData: onRefreshData,);
      }
    );
  } 


  /// 提示确认框，确定
  static showMsgDialog(BuildContext context, {String msg, Function callback}) {
    showDialog(
      context: context,
      barrierDismissible: false, //点击遮罩不关闭对话框
      builder: (context) {
        return AlertDialog(
          contentPadding: const EdgeInsets.all(0),
          actionsPadding: const EdgeInsets.all(0),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(bottom: 23, top: 23),
                child: Text(msg, textAlign: TextAlign.center, style: instanceTextStyle(15, APP_font_black)),
              ),
              Divider(height: 0.5,indent: 12,endIndent: 12,),
              FlatButton(
                onPressed: (){
                  Navigator.of(context).pop();
                  if(callback != null) callback();
                },
                child: Text("确定", style: instanceTextStyle(14, APP_font_black)),
              )
            ],
          ),
        );
      },
    );
  } 

    // 提示确认框，确定取消
  static Future<bool> showConfirmDialog(
    BuildContext context,
    {
      String title = "提示\是否确认",
      String cancel = "取消",
      String sure = "确认",
      Function sureCallback
    }
    ) {
    _currTextStyle(double fontsize, Color color, FontWeight fontWeight) {
      return TextStyle(
                        color: color,
                         fontSize: fontsize,
                         decoration: TextDecoration.none, fontWeight: fontWeight);
    }

    Widget _createBtn(String title, Function action, Color color) {
      return Flexible(
        flex: 1,
        fit: FlexFit.tight,
        child: GestureDetector(
          child: Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 11),
            child: Text(title,style: _currTextStyle(14, color, FontWeight.w300),),
          ),
          onTap: (){
            Navigator.of(context).pop(true);
            if(action != null) action();
          },
        ),
      );
    }

    return showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return Center(
          child: Container(
            margin: const EdgeInsets.only(left: 80, right: 80),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.all(23),
                  child: Text(
                    title,
                    textAlign: TextAlign.center,
                     style: _currTextStyle(15, APP_font_black, FontWeight.w400)
                    ),
                ),
                Divider(color: APP_divider_color, height: 0.5,),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    _createBtn(cancel, null, const Color(0xff999999)),
                    Container(width: 0.5 ,height: 25,color: APP_divider_color),
                    _createBtn(sure, sureCallback, APP_main_color)
                  ],
                )
              ],
            ),
          ),
        );
      },
    );
  }

  /// 简单提示
  static  showToast(String msg) {
    native.Fluttertoast.showToast(
      msg: msg,
      toastLength: native.Toast.LENGTH_SHORT,
      gravity: native.ToastGravity.CENTER,
      backgroundColor: const Color(0xff999999),
      textColor: Colors.white,
      fontSize: 11
    );
  }

  /// 自定义提示
  static showChildDialog(BuildContext context, Widget child, {bool barrierDismissble = true}) {
    showDialog(
      context: context,
      barrierDismissible: barrierDismissble, //点击遮罩默认关闭对话框
      builder: (context) {
        return AlertDialog(
          elevation: 0,
          backgroundColor: Colors.transparent,
          contentPadding: const EdgeInsets.all(0),
          actionsPadding: const EdgeInsets.all(0),
          content: child
        );
      },
    );
  } 

  /// 录音框
  static showVoiceBottomDialog(BuildContext context, {
    Function(String voicepath, int duration) onFinish, 
    Function onCancel,
    int duration,
    String path,
    int minDuraion,
  }) {
    showModalBottomSheet(
      backgroundColor: Colors.transparent,
      context: context,
      builder: (context) {
        return CommAlertVoiceWidget(minDuration: minDuraion, path: path, duration: duration,onCancel: onCancel, onFinish: onFinish,);
      }
    );
  } 


    /// 提示输入框
  static showInputDialog(BuildContext context, TextEditingController textEditingController, {String title, String placeholder, Function(String) sureCallback}) {
    AlertTool.showChildDialog(context, 
      Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10)
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.only(left:23, right: 23, top: 23),
                child: Text(
                  title ?? "添加分组\n请输入新的分组名称",
                  textAlign: TextAlign.center,
                    style: instanceTextStyle(15, APP_font_black)
                  ),
              ),
              Container(
              margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
              height: 30,
              child: TextField(
                  controller: textEditingController,
                  decoration:InputDecoration(
                        hintText: placeholder ?? "请输入~",
                        counterText: "",
                        labelStyle: instanceTextStyle(14, APP_font_black),
                        hintStyle: instanceTextStyle(14, APP_font_grey),
                        focusedBorder: OutlineInputBorder(
                        borderRadius: const BorderRadius.all(Radius.circular(25)),
                        borderSide: const BorderSide(width: .5, color: Colors.black),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderRadius: const BorderRadius.all(Radius.circular(25)),
                        borderSide: const BorderSide(width: .5, color: Colors.black),
                      ),
                      border: OutlineInputBorder(
                        borderRadius: const BorderRadius.all(Radius.circular(25)),
                        borderSide: const BorderSide(width: .5, color: Colors.black),
                      ),
                      contentPadding: const EdgeInsets.only(left: 14)
                  ),
                  autofocus: true,
                ),
              ),
              Divider(color: APP_divider_color, height: 0.5,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  FlatButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 11),
                    child: Text("取消",style: instanceTextStyle(14, const Color(0xff999999)),),
                  ),
                  Container(width: 0.5 ,height: 25,color: APP_divider_color),
                  FlatButton(
                    onPressed: (){
                      Navigator.of(context).pop(true);
                      if(sureCallback != null) sureCallback(textEditingController.text);
                    },
                    child: Text("确定",style: instanceTextStyle(14, APP_main_color),),
                  )
                ],
              )
            ],
          ),
      ),
    );
  }

  // 用户信息
  static showBottomUserInfoDialog(BuildContext context, Widget child,{Function(int) onTapIndex, double evelvation}) {
    final size = MediaQuery.of(context).size;
    showModalBottomSheet(
      backgroundColor: Colors.transparent,
      context: context,
      elevation: evelvation,
      builder: (context) {
        return child;
      }
    );
  } 

}